package com.tbit.uqbike.client.util;

import cn.hutool.core.io.file.FileReader;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.client.util.huawei.Client;
import com.tbit.uqbike.client.util.huawei.Constant;
import com.tbit.uqbike.client.util.huawei.Request;
import com.tbit.uqbike.client.util.huawei.SSLCipherSuiteUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;


/**
 * 华为云 人脸识别和静态活体检测
 *
 * @Author LHX
 * @ClassName FaceCompaerUtil
 * @Description
 * @date 2023/4/3 11:02
 * @Version 1.0
 */

@Slf4j
public class FaceCompareUtil {

    static CloseableHttpClient client;

    static {
        try {
            client = (CloseableHttpClient) SSLCipherSuiteUtil.createHttpClient(Constant.INTERNATIONAL_PROTOCOL);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


    public static String doPostHuawei(Map<String, String> map) {
        long one = System.currentTimeMillis();
        String result = "";
        Request request = new Request();
        try {
            request.setKey(map.get("ak"));
            request.setSecret(map.get("sk"));
            request.setUrl(map.get("url"));
            request.setMethod("POST");
            request.addHeader("Content-Type", "application/json");
            request.setBody(map.get("jsonStr"));
        } catch (Exception e) {
            log.info(e.getMessage());
            log.error(e.getMessage(), e);
        }
        try {
            long start = System.currentTimeMillis();
            HttpRequestBase signedRequest = Client.sign(request, Constant.SIGNATURE_ALGORITHM_SDK_HMAC_SHA256);
            long end = System.currentTimeMillis();
            log.debug("加密消耗时间:" + (end - start));
            HttpResponse response = client.execute(signedRequest);
            long last = System.currentTimeMillis();
            log.debug("请求消耗时间:" + (last - end));
            Header[] resHeaders = response.getHeaders("X-Request-Id");
            for (Header h : resHeaders) {
                log.info("Processing Header with name: {} and value: {}" + h.getName() + h.getValue());
            }
            HttpEntity responseBody = response.getEntity();
            if (responseBody == null) {
                return result;
            } else {
                byte[] byteStream = EntityUtils.toByteArray(responseBody);
                System.arraycopy(byteStream, 0, new byte[byteStream.length], 0, byteStream.length);
                result = new String(byteStream, StandardCharsets.UTF_8);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        long two = System.currentTimeMillis();
        log.debug("接口请求时间:" + (two - one));
        return result;
    }


    /**
     * 华为云静态活体识别
     *
     * @param imageBase64
     * @return
     */
    public static String liveDetect(String imageBase64, Map<String, String> map) {
        String project = map.get("project");
        String projectId = map.get("projectId");
        String url = String.format("https://face.%s.myhuaweicloud.com/v1/%s/live-detect-face?image_file", project, projectId);
        Map<String, String> jsonMap = new HashMap<>();
        jsonMap.put("image_base64", imageBase64);
        map.put("url", url);
        map.put("jsonStr", JSON.toJSONString(jsonMap));
        //调用静态活体检测
        return doPostHuawei(map);

    }

    /**
     * 华为云人脸识别
     *
     * @param
     */
    public static String faceCompare(String image1Base64, String image2Base64, Map<String, String> map) {
        String project = map.get("project");
        String projectId = map.get("projectId");
        String url = String.format("https://face.%s.myhuaweicloud.com/v2/%s/face-compare", project, projectId);
        Map<String, String> jsonMap = new HashMap<>();
        jsonMap.put("image1_base64", image1Base64);
        jsonMap.put("image2_base64", image2Base64);
        map.put("url", url);
        map.put("jsonStr", JSON.toJSONString(jsonMap));
        return doPostHuawei(map);

    }


    public static void main(String[] args) {
        FileReader fileReader1 = new FileReader("C:\\Users\\admin\\Desktop\\test1.txt");
        FileReader fileReader2 = new FileReader("C:\\Users\\admin\\Desktop\\test2.txt");
        String s1 = fileReader1.readString();
        String s2 = fileReader2.readString();
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, String> jsonMap = new HashMap<>();
        jsonMap.put("image1_base64", s1);
        jsonMap.put("image2_base64", s2);
//        String url = "https://face.cn-north-4.myhuaweicloud.com/v1/d2c6a1c1c131455db2fe4399c38b6e1d/live-detect-face?image_file";
//        String url = "https://face.cn-north-4.myhuaweicloud.com/v1/045b7f62b0324b1380a8af70c9df6661/live-detect-face?image_file";
        String url = "https://face.cn-north-4.myhuaweicloud.com/v2/045b7f62b0324b1380a8af70c9df6661/face-compare";
        map.put("image_base64", JSON.toJSONString(jsonMap));
        map.put("url", url);
        map.put("jsonStr", JSON.toJSONString(jsonMap));
//        map.put("ak", "RNM9RZXB1DW8RUR2BD3M");
//        map.put("sk", "oXNF1yUCReTOh8T67kQSfvOuKY7Sz2TH79J3kVeT");
        map.put("ak", "FRIUC7908MZROMY1A5ZT");
        map.put("sk", "vZB5vgK2a5nRw7ahH7U8wt8hUszKcApOeBPsNyx3");
        log.info("华为云静态活体检测调用参数:" + map.toString());
        String s = doPostHuawei(map);
        System.out.println("请求结果:" + s);

    }
}
